<?php

class Expenses extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    function insert($input,$is_array=false){
        if($is_array){
            foreach ($input as $expense) {
                if(strcmp('', $expense['title']) != 0){
                    $this->insert_single($expense);
                }
            }
        } else {
            $this->insert_single($input);
        }
    }
    
    function insert_single($input){
        $this->load->library('session');
        date_default_timezone_set("Asia/Jakarta");
        $this->db->set('user_id', $this->session->userdata('user_id'));
        $this->db->set('category_id', $input['category']);
        $this->db->set('expense_title', $input['title']);
        $this->db->set('expense_amount', $input['amount']);
        $this->db->set('expense_date', date("Y-m-d H:i:s"));
        $this->db->insert('expense'); 
    }
    
    function get_last_item(){
        $this->db->join('category', 'category.category_id = expense.category_id');
        $this->db->where('expense.user_id',$this->session->userdata('user_id'));
        $this->db->limit(10);
        $this->db->order_by("expense_date", "desc"); 
        return $this->db->get('expense');
    }
    
    function get_this_month(){
        date_default_timezone_set("Asia/Jakarta");
        $start = date("Y-m-01");
        $end = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
        $this->db->select_sum('expense_amount');
        $where = "user_id=".$this->session->userdata('user_id')." AND CAST(expense_date AS DATE) BETWEEN '".$start."' AND '".$end."';";
        $this->db->where($where,NULL,FALSE);
        $query = $this->db->get('expense');
        return $query->first_row()->expense_amount;
    }
    
    function get_limit($start,$count){
        $this->db->join('category', 'category.category_id = expense.category_id');
        $this->db->where('expense.user_id',$this->session->userdata('user_id'));
        $this->db->limit($count,$start);
        $this->db->order_by("expense_date", "desc"); 
        return $this->db->get('expense');
    }
    
    function delete($id){
        $this->db->where('expense_id',$id);
        $this->db->delete('expense');
        if($this->db->affected_rows() > 0){
            return 'true';
        }else{
            return 'false';
        }
    }
    
    function get_category(){
        $this->db->where('user_id',$this->session->userdata('user_id'));
        $query = $this->db->get('category');
        return $query;
    }
    
    function get_category_by_id($cat_id){
        $this->db->where('category_id',$cat_id);
        $query = $this->db->get('category');
        return $query->first_row();
    }
    
    function get_category_by_user_id($user_id){
        $this->db->where('user_id',$user_id);
        $query = $this->db->get('category');
        return $query;
    }
    
    function get_category_owner($cat_id){
        $this->db->where('category_id',$cat_id);
        $query = $this->db->get('category');
        return $query->first_row()->user_id;
    }
    
    function insert_category($title){
        $this->db->set('user_id', $this->session->userdata('user_id'));
        $this->db->set('category_title',$title);
        $this->db->insert('category');
    }
    
    function update_category($id,$title){
        $this->db->where('category_id',$id);
        $this->db->set('category_title',$title);
        $this->db->update('category');
    }
    
    function delete_category($cat_id){
        $this->db->where('category_id',$cat_id);
        $this->db->delete('category'); 
    }
}

?>